import { Component, OnInit } from '@angular/core';
import {AbstractControl, FormControl, FormGroup, NonNullableFormBuilder, ValidatorFn, Validators} from "@angular/forms";
import {NzFormTooltipIcon} from "ng-zorro-antd/form";
import {HttpService} from "../../service/http/http.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {NzModalService} from "ng-zorro-antd/modal";

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  okLoading = false;
  tableLoading = false;
  listOfData: any[] = [];
  page = 1;
  size = 10;
  total = 0;
  validateForm: FormGroup<{
    id:FormControl<number>;
    shopIds:FormControl<any>;
    moblie: FormControl<string>;
    password: FormControl<string>;
    name: FormControl<string>;
  }>;

  shops:any[] = [];

  shopsValue = [];
  modalTitle = "";

  constructor(private fb: NonNullableFormBuilder,
              private msg:NzMessageService,
              private modal: NzModalService,
              private http:HttpService) {
    this.validateForm = this.fb.group({
      id: [0],
      shopIds: [undefined,[Validators.required]],
      moblie: ['', [Validators.required]],
      password: ['', [Validators.required]],
      name: ['', [Validators.required]],
    });
  }

  ngOnInit() {
    this.getList();
    this.getAllShops();
  }

  getAllShops(){

    let url = "/shop/getAll";
    this.http.get(url,{}).then((res:any)=>{
      this.shops = res;
    })
  }

  blurFund(e:any,data:any){
    if (!data.funds || data.funds <= 0){
      this.msg.warning("新金额不能小于原金额");
      return
    }

    this.modal.create({
      nzTitle:"今日限额为",
      nzContent:data.funds+"元",
      nzMaskClosable:false,
      nzOkText:"确认",
      nzOnOk:()=>{
        let params = {
          id:data.id,
          funds:data.funds,
          shopIds:[]
        }
        let url = "/user/edit"
        this.http.postBody(url,params).then(res=>{
          this.msg.success("修改成功");
          this.getList();
        }).catch(err=>{
          this.okLoading = false;
          this.msg.error(err);
        });
      }
    });
  }

  getList(){
    let params = {
      pageNumber:this.page,
      pageSize:this.size
    }
    this.tableLoading = true;
    let url = "/user/getAllUser";
    this.http.get(url,params).then((res:any)=>{
      this.tableLoading = false;
      this.listOfData = res.records;
      this.total = res.total;
    }).catch(err=>{
      this.tableLoading = false;
    });
  }

  isVisible = false;
  add(){
    this.isEdit = false;
    this.modalTitle = "新增用户"
    this.isVisible = true;
    this.validateForm.reset();
  }

  handleCancel(){
    this.isVisible = false;
  }

  deletedUser(item:any){
    let url = "/user/deleted"
    this.http.get(url,{id:item.id}).then(res=>{
      this.msg.success("删除成功");
      this.getList();
    }).catch(err=>{
      this.msg.error(err);
    });
  }

  changePage(p:any){
    this.page = p;
    this.getList();
  }

  private isEdit = false;
  edit(data:any){
    this.modalTitle = "修改用户"
    let ids:any[] = [];
    data.shops.forEach((item:any)=>{
      ids.push(item.id+"");
    })
    this.validateForm.reset({
      id:data.id,
      shopIds:ids,
      moblie: data.moblie,
      password:data.password,
      name: data.name,
    });
    this.isEdit = true;
    this.isVisible = true;
  }

  handleOk(){



    if (this.validateForm.valid) {
      let value = this.validateForm.value;
      let url = "/user/add"
      if (this.isEdit){
        url = "/user/edit"
      }
      this.okLoading = true;
      this.http.postBody(url,value).then(res=>{
        this.okLoading = false;
        this.isVisible = false;
        if (this.isEdit){
          this.msg.success("修改成功");
        }else {
          this.msg.success("添加成功");
        }
        this.getList();
      }).catch(err=>{
        this.okLoading = false;
        this.msg.error(err);
      });
    } else {
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }

  }

}
